/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database.dao;

import database.common.GenericDao;
import database.entity.Distributors;
import database.entity.Purchases;
import java.util.Date;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Administrator
 */
public class PurchasesDao extends GenericDao<Purchases, Integer> {

    public List<Purchases> search(Date start, Date end, String purchaseName, String distributorName, String distributorType) {
        List<Purchases> list;
        Session session = sessionFactory.getCurrentSession();
        session.beginTransaction();
        Criteria c = session.createCriteria(Purchases.class);
         c.createAlias("distributor", "distributor_alias");
        if (distributorName != null && !distributorName.isEmpty()) {
            c.add(Restrictions.disjunction().add(Restrictions.like("distributor_alias.name", "%" + distributorName + "%")));
        }
        if (purchaseName != null && !purchaseName.isEmpty()) {
            c.add(Restrictions.like("name", "%" + purchaseName + "%"));
        }
        if (start != null) {
            c.add(Restrictions.ge("time", start));
        }
        if (end != null) {
            c.add(Restrictions.le("time", end));
        }
        if (!distributorType.equals("All")) {
            c.add(Restrictions.disjunction().add(Restrictions.eq("distributor_alias.type", distributorType)));
        }
        c.add(Restrictions.eq("status", 0));
        list = c.list();
        session.flush();
        session.getTransaction().commit();
        return list;
    }

    public List<Purchases> getAllAvaiable() {
        List<Purchases> list;
        Session session = sessionFactory.getCurrentSession();
        session.beginTransaction();
        Criteria c = session.createCriteria(Purchases.class);
        c.add(Restrictions.eq("status", 0));
        list = c.list();
        session.flush();
        session.getTransaction().commit();
        return list;
    }

    public List<Purchases> getAllByDis(Distributors aThis) {
        List<Purchases> list;
        Session session = sessionFactory.getCurrentSession();
        session.beginTransaction();
        Criteria c = session.createCriteria(Purchases.class);
        c.add(Restrictions.eq("distributor", aThis));
        list = c.list();
        session.flush();
        session.getTransaction().commit();
        return list;
    }
}
